package com.hospital.common.utils;

import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ExtractUtil
 * @Description 描述
 * @Author sjc
 * @Date 2024/10/19 13:44
 */
@Component
@Log4j2
public class ExtractUtil {

    /**
     * 根据语句中提取病状
     */
    public static String extract(String str) {
//        String patientDescription = "我最近腿疼,腰疼得厉害，还伴有脚气伴有糜水泡烂。";
        if("你好".equals(str)||"您好".equals(str)){
            return "您好,请详细描述您的病状,格式为:我最近腿疼,腰疼得厉害，还伴有脚气伴有糜水泡烂.";
        }
        List<String> extractedSymptoms = extractSymptoms(str);
        //数组变字符串
        String extractedSymptomsString = String.join(",", extractedSymptoms);
        if("".equals(extractedSymptomsString)){
            return "您好,请详细描述您的病状,格式为:我最近腿疼,腰疼得厉害，还伴有脚气伴有糜水泡烂.";
        }
        String substring = extractedSymptomsString.substring(1);
        System.out.println("提取到的病状:" + substring);
        return substring;
    }


    /**
     * 读取文件
     * @param patientDescription
     * @return
     */
    public static List<String> extractSymptoms(String patientDescription) {
        List<String> symptoms = new ArrayList<>();
        try{
            BufferedReader reader = new BufferedReader(new FileReader("elt-modules/elt-patient/src/main/resources/sickness.txt"));
            String line;
            while ((line = reader.readLine())!= null) {
                if (patientDescription.contains(line)) {
                    symptoms.add(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return symptoms;
    }

}
